package com.czwj.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.dao.AttachmentMapper;
import com.czwj.dao.AttachmentRelyMapper;
import com.czwj.dao.AttendanceMapper;
import com.czwj.dao.DutyAttendanceMapper;
import com.czwj.dao.ProjectItemMapper;
import com.czwj.entities.Attachment;
import com.czwj.entities.AttachmentRely;
import com.czwj.entities.Attendance;
import com.czwj.entities.DutyAttendance;
import com.czwj.entities.ProjectItem;
import com.czwj.service.AttendanceService;

@Service
public class AttendanceServiceImpl implements AttendanceService {

	@Resource
	AttendanceMapper mapper;
	@Resource
	AttachmentMapper amapper;
	@Resource
	AttachmentRelyMapper relymapper;
	@Resource
	ProjectItemMapper itemmapper;
	@Resource
	DutyAttendanceMapper dutymapper;
	
	@Value("${resources.phypath}")
	private String PhyPath;

	@Value("${resources.respath}")
	private String ResPath;
	
	private final String Attachment_Attendance_Path = "考勤/";
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	public List<String> existattendance(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.existattendance(map);
	}

	@Override
	public Attendance findonday(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return this.mapper.findoneday(map);
	}

	@Override
	public int insert(Attendance attendance, MultipartFile[] images ,int[] duty) {
		// TODO Auto-generated method stub
		int count = this.mapper.insertSelective(attendance);
		for(int i:duty){
			DutyAttendance dutyAttendance = new DutyAttendance();
			dutyAttendance.setAttendanceid(attendance.getId());
			dutyAttendance.setUserid(i);
			this.dutymapper.insertSelective(dutyAttendance);
		}
		String date = sdf.format(attendance.getDate());
		ProjectItem item = this.itemmapper.selectByPrimaryKey(attendance.getProjectitemid());
		String path = item.getName()+"/"+Attachment_Attendance_Path+date+"/";
		String sourcepath = PhyPath+path;//存储路径
		String urlpath = ResPath+path;//资源路径
		File dest = new File(sourcepath);
		if (!dest.exists()) {
			dest.mkdirs();
		}
		for (MultipartFile image : images) {
			String filename = image.getOriginalFilename();
			if(!filename.contains(".")) {
				filename+=".jpg";
			}
			String _sourcepath = sourcepath+filename;
			String _urlpath =urlpath+filename;
			File file = new File(_sourcepath);
			while (file.exists()) {
				String[] arr =  _sourcepath.split("\\.");
				_sourcepath="";
				for(int i=0;i<arr.length;i++){
					if(i<arr.length-1){
						_sourcepath += arr[i];;
					}else{
						_sourcepath+="(1)."+arr[i];
					}
				}
				file = new File(_sourcepath);
			}
			try {
				image.transferTo(new File(_sourcepath));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return 0;
			}
			Attachment attachment = new Attachment(filename, _urlpath, _sourcepath, "image", attendance.getCreaterid(), new Date(), attendance.getCreateip());
			count+=this.amapper.insertSelective(attachment);
			AttachmentRely aRely = new AttachmentRely("attendance", attendance.getId(), attachment.getId());
			count+=this.relymapper.insertSelective(aRely);
		}
		return count;
	}
	
	
}
